import Taro from '@tarojs/taro'

import {serverDomain,port} from '../config/config'
import {ADD_MISSION,POP_MISSION,SUBMIT_MISSION,SUBMIT_PLAN,SUBMIT_PLAN_SUCCESS,CANCEL_OVERRIDE_PLAN} from '../constants/makePlan'

export function onAddMission(){
    return {
        type:ADD_MISSION
    }
}

export function onSubmitMission(data){
    return {
        type:SUBMIT_MISSION,
        payload:data
    }
}

export function onPopMission(){
    return {
        type:POP_MISSION
    }
}

export function onSubmitPlan(data,cb){
    return dispatch=>{
        Taro.showToast({
            title:'正在提交计划...',
            icon:'loading',
            mask:true
        })
            .then(()=>{
                Taro.request({
                    url:`${serverDomain}:${port}/plan/makePlan`,
                    method:'POST',
                    data:data
                }).then((res)=>{
                    if(res.data.code===0){
                        dispatch({
                            type:SUBMIT_PLAN,
                            payload:res
                        });
                        cb&&cb();
                    }
                    else{
                        Taro.atMessage({
                            message:'服务器出错',
                            type:'error',
                            duration:3000
                        })
                    }
                     Taro.hideToast(); 
                })
            })
    }
}

export function onSubmitPlanSuccess(){
    return {
        type:SUBMIT_PLAN_SUCCESS
    }
}

export function onCancelOverridePlan(){
    return {
        type:CANCEL_OVERRIDE_PLAN
    }
}